<?php
namespace app\api\controller;

class Auth2 extends Base
{

    public function access_token()
    {
        $return['success'] = true;
        $posts['site_id'] = $this->ys_config['siteId'];
        $get_chk = $this->sendInfo('getSite', $posts);
        if($get_chk['data']['expires_stop_time'] > time()){
            $return['success'] = true;
            $return['access_token'] = $get_chk['data']['access_token'];
            $return['expires_at'] = date('Y-m-d H:i:s',$get_chk['data']['expires_stop_time']);
            $this->ajaxReturn($return);
        } else {
            if ($this->request['username'] != $this->username) {
                $return['success'] = false;
                $return['desc'] = '账户错误';
                $this->ajaxReturn($return);
            }
            $password = strtolower(md5($this->password));
            if ($this->request['password'] != $this->password) {
                $return['success'] = false;
                $return['desc'] = '密码错误';
                $this->ajaxReturn($return);
            }
            $sign = strtolower(md5($this->request['username'] . $this->password . $this->request['timestamp'] . $this->password));
            if ($sign != $this->request['sign']) {
                $return['success'] = false;
                $return['desc'] = 'sign错误';
                $this->ajaxReturn($return);
            }
            $post['access_token'] = md5($this->ys_config['siteId'] . time());
            $post['expires_stop_time'] = strtotime($this->request['timestamp']) + (60 * 60 * 12);
            $data = $this->sendInfo('accessToken', $post);
            if ($data['code'] == 1) {
                $return['success'] = true;
                $return['access_token'] = $post['access_token'];
                $return['expires_at'] = date('Y-m-d H:i:s',$post['expires_stop_time']);
                $this->ajaxReturn($return);
            }
        }
    }
}